package com.tanhua.server.controller;

import com.tanhua.model.domain.PageResult;
import com.tanhua.server.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author lk
 * @date 2022-03-28
 */
@RestController
@RequestMapping("/comments")
public class CommentController {
    @Autowired
    private CommentService commentService;

    //分页查询评论列表
    @GetMapping
    public ResponseEntity findComments(@RequestParam(defaultValue = "1") Integer page,
                                       @RequestParam(defaultValue = "10") Integer pagesize,
                                       String movementId) {
        PageResult pageResult = commentService.findComments(page,pagesize,movementId);
        return ResponseEntity.ok(pageResult);
    }

    //发布评论
    @PostMapping
    public ResponseEntity saveComments(@RequestBody Map map){
        //获取动态id
        String movementId = (String) map.get("movementId");
        //获取评论
        String comment = (String) map.get("comment");
        commentService.saveComments(movementId,comment);
        return ResponseEntity.ok(null);
    }

    //评论点赞
    @GetMapping("/{id}/like")
    public ResponseEntity commentLike(@PathVariable("id") String id){
        Integer likeComment = commentService.findLikeComment(id);
        return ResponseEntity.ok(likeComment);
    }

    //取消评论点赞
    @GetMapping("/{id}/dislike")
    public ResponseEntity disCommentLike(@PathVariable("id") String id){
        Integer likeComment = commentService.findDisLikeComment(id);
        return ResponseEntity.ok(likeComment);
    }
}
